package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.http.HttpSession;

import com.erpanderp.wam.adm.model.entity.Permission;
import com.erpanderp.wam.adm.model.entity.Role;
import com.erpanderp.wam.adm.model.entity.RolePermission;
import com.erpanderp.wam.adm.model.service.PermissionDAO;
import com.erpanderp.wam.adm.model.service.RoleDAO;
import com.erpanderp.wam.adm.model.service.RolePermissionDAO;
import com.erpanderp.wam.usm.model.entity.timesheet.Timesheet;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;

@ManagedBean
@ViewScoped
public class RolePermissionsBean implements Serializable{

	private static final long serialVersionUID = 1L;

	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	
	private List<RolePermission> rolePermissions = new ArrayList<RolePermission>();
	private Map<String, Long> rolesMap = new HashMap<String, Long>();
	private Long selectedRoleId;
	private Role role;
	private RolePermission rolePermission;
	private boolean editEmployeeInfoPermission;
	RolePermissionDAO rolePermissionDAO = new RolePermissionDAO();
	RoleDAO roleDAO = new RoleDAO();	
	PermissionDAO permissionDAO = new PermissionDAO();
	private boolean renderRolePermissionTable;
	
	public RolePermissionsBean(){
		List list = roleDAO.getAllRoles();
		for(Role role : (List<Role>) list){
			rolesMap.put(role.getDisplayName(), role.getRoleId());
		}
	}

	public void roleSelectionListener(AjaxBehaviorEvent e){
		System.out.println(selectedRoleId);
		rolePermissions = rolePermissionDAO.getAllRolePermissionByRoleId(selectedRoleId);
		List<Permission> permissions = permissionDAO.getAllPermissions();
		role = roleDAO.getRoleByRoleID(selectedRoleId);
		for(Permission permission : permissions){
			boolean ROLE_PERMISSION_FOUND = false;
			for(RolePermission rolePermission : rolePermissions){
				if(rolePermission.getPermission().getPermissionId() == permission.getPermissionId()){
					ROLE_PERMISSION_FOUND = true;
					break;
				}
			}
			if(!ROLE_PERMISSION_FOUND){
				RolePermission rolePermission = new RolePermission();
				rolePermission.setCreateDate(new Date());
				rolePermission.setCreatedBy(wamUser.getEmployee().getFirstName());
				rolePermission.setPermission(permission);
				rolePermission.setPermitted(CommonLookup.PERMISSION_NO);
				rolePermission.setRole(role);
				rolePermission.setStatus(CommonLookup.STATUS_ACTIVE);
				rolePermissionDAO.createRolePermission(rolePermission);
				ROLE_PERMISSION_FOUND = false;
			}
		}
		rolePermissions = rolePermissionDAO.getAllRolePermissionByRoleId(selectedRoleId);
		if(rolePermissions != null && rolePermissions.size() > 0){
			setRenderRolePermissionTable(true);
		}else{
			setRenderRolePermissionTable(false);
		}
	}
	
	public void changePermission(AjaxBehaviorEvent e){
		rolePermission = (RolePermission) e.getComponent().getAttributes().get("rolePermission");
		rolePermission.setDateModified(new Date());
		rolePermission.setModifiedBy(wamUser.getEmployee().getFirstName());
		if(rolePermissionDAO.updateRolePermission(rolePermission) == null){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Failed to update permission for the selected role. Please try again.", ""));
		}else{
			System.out.println("UPDATED");
		}
	}
	
	
	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public boolean isEditEmployeeInfoPermission() {
		return editEmployeeInfoPermission;
	}

	public void setEditEmployeeInfoPermission(boolean editEmployeeInfoPermission) {
		this.editEmployeeInfoPermission = editEmployeeInfoPermission;
	}

	public Map<String, Long> getRolesMap() {
		return rolesMap;
	}

	public void setRolesMap(Map<String, Long> rolesMap) {
		this.rolesMap = rolesMap;
	}

	public Long getSelectedRoleId() {
		return selectedRoleId;
	}

	public void setSelectedRoleId(Long selectedRoleId) {
		this.selectedRoleId = selectedRoleId;
	}

	public List<RolePermission> getRolePermissions() {
		return rolePermissions;
	}

	public void setRolePermissions(List<RolePermission> rolePermissions) {
		this.rolePermissions = rolePermissions;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public boolean isRenderRolePermissionTable() {
		return renderRolePermissionTable;
	}

	public void setRenderRolePermissionTable(boolean renderRolePermissionTable) {
		this.renderRolePermissionTable = renderRolePermissionTable;
	}

	public RolePermission getRolePermission() {
		return rolePermission;
	}

	public void setRolePermission(RolePermission rolePermission) {
		this.rolePermission = rolePermission;
	}

}
